﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DatNuocViet.Models;

namespace DatNuocViet.Areas.Admin.Controllers
{
    [CustomAuthentication]
    public class AdminNewsController : Controller
    {
        #region newstype
        public ActionResult NewsType()
        {
            List<GetNewsTypeResult> model = new List<GetNewsTypeResult>();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                model = db.GetNewsType().ToList();
            }
            return View(model);
        }

        public ActionResult CreateNewsType()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateNewsType(FormCollection form)
        {
            String NewsTypeVn = form["NewsTypeVn"].Trim();
            String NewsTypeEn = form["NewsTypeEn"].Trim();
            String SortOrder = form["SortOrder"].Trim();
            if (ValidateNewsType(NewsTypeVn, NewsTypeEn, SortOrder))
            {
                using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
                {
                    db.InsertNewsType(NewsTypeVn, NewsTypeEn, Convert.ToInt32(SortOrder));
                }
                return Content("<script type='text/javascript'>alert('Thêm loại tin thành công');window.location.replace('/Admin/AdminNews/NewsType');</script>");
            }
            ViewBag.message = "Dữ liệu loại tin không đúng";
            return View();
        }

        public ActionResult EditNewsType(int id)
        {
            NewsTypeDetailResult model = new NewsTypeDetailResult();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                model = db.NewsTypeDetail(id).SingleOrDefault();
            }
            if (model == null)
                return Redirect(Url.Content("~/Admin/AdminNews/NewsType"));
            else
                return View(model);
        }

        [HttpPost]
        public ActionResult EditNewsType(FormCollection form)
        {
            String NewsTypeVn = form["NewsTypeVn"].Trim();
            String NewsTypeEn = form["NewsTypeEn"].Trim();
            String SortOrder = form["SortOrder"].Trim();
            if (ValidateNewsType(NewsTypeVn, NewsTypeEn, SortOrder))
            {
                try
                {
                    int Id = Convert.ToInt32(form["Id"]);
                    bool Delete = form["Delete"] == "on" ? true : false;
                    NewsTypeDetailResult model = new NewsTypeDetailResult();
                    using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
                    {
                        db.UpdateNewsType(Id, NewsTypeVn, NewsTypeEn, Convert.ToInt32(SortOrder), Delete);
                        model = db.NewsTypeDetail(Id).SingleOrDefault();
                    }
                    if (Delete == true)
                        return Content("<script type='text/javascript'>alert('Đã xóa loại tin');window.location.replace('/Admin/AdminNews/NewsType');</script>");
                    else
                    {
                        ViewBag.message = "Chỉnh sửa loại tin thành công";
                        return View(model);
                    }
                }
                catch (Exception)
                {
                    return Redirect(Url.Content("~/Admin/AdminNews/NewsType"));
                }
            }
            int Id1 = Convert.ToInt32(form["Id"]);
            NewsTypeDetailResult model1 = new NewsTypeDetailResult();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                model1 = db.NewsTypeDetail(Id1).SingleOrDefault();
            }
            ViewBag.message = "Dữ liệu loại tin không đúng";
            return View(model1);
        }
        #endregion

        #region news

        public ActionResult News()
        {
            int PageIndex = 1;
            string KeyWord = string.Empty;
            int NewsType = -1;
            try
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
                if (PageIndex == 0)
                    PageIndex = 1;
            }
            catch (Exception)
            {
                PageIndex = 1;
            }
            if (Request.QueryString["KeyWord"] != null)
            {
                KeyWord = Request.QueryString["KeyWord"].ToString();
            }
            try
            {
                NewsType = Convert.ToInt32(Request.QueryString["NewsType"]);
                if (NewsType == 0)
                    NewsType = -1;
            }
            catch (Exception)
            {
                NewsType = -1;
            }
            List<SearchNewsResult> model = new List<SearchNewsResult>();
            List<GetNewsTypeResult> newsType = new List<GetNewsTypeResult>();
            int totalPage = 0;
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                model = db.SearchNews(PageIndex, 20, KeyWord, NewsType, "[News].[TitleVn]", "asc", "vn").ToList();
                totalPage = db.SearchNews(-1, -1, KeyWord, NewsType, "[News].[TitleVn]", "asc", "vn").Count();
                if (totalPage % 20 == 0)
                    totalPage = totalPage / 20;
                else
                    totalPage = totalPage / 20 + 1;
                if (totalPage == 0)
                    totalPage = 1;
                newsType = db.GetNewsType().OrderBy(t => t.NewsTypeVn).ToList();
            }
            ViewBag.NewsType = newsType;
            ViewBag.TotalPage = totalPage;
            ViewBag.KeyWord = KeyWord;
            ViewBag.CurrentType = NewsType;
            ViewBag.PageIndex = PageIndex;
            return View(model);
        }

        public ActionResult CreateNews()
        {
            List<GetNewsTypeResult> newsType = new List<GetNewsTypeResult>();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                newsType = db.GetNewsType().ToList();
            }
            ViewBag.NewsType = newsType;
            return View();
        }

        [HttpPost]
        public ActionResult CreateNews(FormCollection form)
        {
            string TitleVn = form["TitleVn"].Trim();
            string TitleEn = form["TitleEn"].Trim();
            string DescriptionVn = form["DescriptionVn"].Trim();
            string DescriptionEn = form["DescriptionEn"].Trim();
            string DetailsVn = form["DetailsVn"];
            string DetailsEn = form["DetailsEn"];
            string NewsType = form["NewsType"];
            string NewsImage = form["NewsImage"];
            if (ValidateNews(TitleVn, NewsType))
            {
                using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
                {
                    db.InsertNews(TitleVn, TitleEn, DescriptionVn, DescriptionEn, HttpUtility.HtmlDecode(DetailsVn), HttpUtility.HtmlDecode(DetailsEn), NewsImage, Convert.ToInt32(NewsType));
                }
                return Content("<script type='text/javascript'>alert('Thêm bài viết thành công');window.location.replace('/Admin/AdminNews/News');</script>");
            }
            ViewBag.message = "Dữ liệu bài viết không đúng";
            List<GetNewsTypeResult> newsType = new List<GetNewsTypeResult>();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                newsType = db.GetNewsType().ToList();
            }
            ViewBag.NewsType = newsType;
            return View();
        }

        public ActionResult EditNews(int id)
        {
            NewsDetailsResult model = new NewsDetailsResult();
            List<GetNewsTypeResult> newsType = new List<GetNewsTypeResult>();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                model = db.NewsDetails(id).SingleOrDefault();
                newsType = db.GetNewsType().ToList();
            }
            if (model == null)
                return Redirect(Url.Content("~/Admin/AdminNews/News"));
            else
            {
                ViewBag.NewsType = newsType;
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult EditNews(FormCollection form)
        {
            string TitleVn = form["TitleVn"].Trim();
            string TitleEn = form["TitleEn"].Trim();
            string DescriptionVn = form["DescriptionVn"].Trim();
            string DescriptionEn = form["DescriptionEn"].Trim();
            string DetailsVn = form["DetailsVn"];
            string DetailsEn = form["DetailsEn"];
            string NewsType = form["NewsType"];
            string NewsImage = form["NewsImage"];
            if (ValidateNews(TitleVn, NewsType))
            {
                try
                {
                    int Id = Convert.ToInt32(form["Id"]);
                    bool Delete = form["Delete"] == "on" ? true : false;
                    NewsDetailsResult model = new NewsDetailsResult();
                    List<GetNewsTypeResult> newsType = new List<GetNewsTypeResult>();
                    using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
                    {
                        db.UpdatetNews(Id, TitleVn, TitleEn, DescriptionVn, DescriptionEn, HttpUtility.HtmlDecode(DetailsVn), HttpUtility.HtmlDecode(DetailsEn), NewsImage, Delete, Convert.ToInt32(NewsType));
                        model = db.NewsDetails(Id).SingleOrDefault();
                        newsType = db.GetNewsType().ToList();
                    }
                    if (Delete == true)
                        return Content("<script type='text/javascript'>alert('Đã xóa bài viết');window.location.replace('/Admin/AdminNews/News');</script>");
                    else
                    {
                        ViewBag.NewsType = newsType;
                        ViewBag.message = "Chỉnh sửa bài viết thành công";
                        return View(model);
                    }
                }
                catch (Exception)
                {
                    return Redirect(Url.Content("~/Admin/AdminNews/News"));
                }
            }
            int Id1 = Convert.ToInt32(form["Id"]);
            NewsDetailsResult model1 = new NewsDetailsResult();
            List<GetNewsTypeResult> newsType1 = new List<GetNewsTypeResult>();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                newsType1 = db.GetNewsType().ToList();
                model1 = db.NewsDetails(Id1).SingleOrDefault();
            }
            ViewBag.NewsType = newsType1;
            ViewBag.message = "Dữ liệu bài viết không đúng";
            return View(model1);
        }

        #endregion

        #region validate
        [NonAction]
        private bool ValidateNewsType(string NewsTypeVn, string NewsTypeEn, string SortOrder)
        {
            if (string.IsNullOrEmpty(NewsTypeVn))
                return false;
            if (string.IsNullOrEmpty(NewsTypeEn))
                return false;
            try
            {
                int number = Convert.ToInt32(SortOrder);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        [NonAction]
        private bool ValidateNews(string TitleVn, string NewsType)
        {
            if (string.IsNullOrEmpty(TitleVn))
                return false;
            try
            {
                int number = Convert.ToInt32(NewsType);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        #endregion
    }
}
